package com.yhb.channel.impl.ANYPAY.http.utils;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

public class AnyPaySignUtils {

    /**
     *  签名
     * @param privateKey pk
     * @param data data
     * @param algorithm 算法
     * @return
     */
    public static byte[] sign(PrivateKey privateKey, byte[] data, String algorithm) {
        try {
            Signature signature = Signature.getInstance(algorithm);
            signature.initSign(privateKey);
            signature.update(data);
            byte[] sign = signature.sign();
            return sign;
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }

    /**
     * 验签
     * @param publicKey pk
     * @param data data
     * @param sign 签名
     * @param algorithm 算法
     * @return
     */
    public static boolean verifySign(PublicKey publicKey, byte[] data, byte[] sign, String algorithm) {
        try {
            Signature signature = Signature.getInstance(algorithm);
            signature.initVerify(publicKey);
            signature.update(data);
            return signature.verify(sign);
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }
}
